﻿using AutoMapper;
using CaltechSoft.Service.DBCommand;
using CaltechSoft.Service.EntityFW.Repository;
using CaltechSoft.Service.Infrastructure;
using CaltechSoft.Service.Models;
using CaltechSoft.Service.ReportService.Certificate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CaltechSoft.Service.ReportService
{
    public class CertificateReportService : ICertificateReportService
    {
        private readonly ICommandExecutor _commandExecutor;
        private readonly IGenericRepository<EntityFW.Entity.Certificate> _certificateGenericRepository;
        private readonly IGenericRepository<EntityFW.Entity.Customer> _customerGenericRepository;
        private readonly IGenericRepository<EntityFW.Entity.Equipment> _equipmentGenericRepository;
        private readonly IGenericRepository<EntityFW.Entity.CertificateDetails> _certificateDetailsGenericRepository;
        private readonly IGenericRepository<EntityFW.Entity.CertificateStandard> _certificateStandardGenericRepository;

        public CertificateReportService(ICommandExecutor commandExecutor, IGenericRepository<EntityFW.Entity.Certificate> certificateGenericRepository,
            IGenericRepository<EntityFW.Entity.Customer> customerGenericRepository, IGenericRepository<EntityFW.Entity.Equipment> equipmentGenericRepository,
            IGenericRepository<EntityFW.Entity.CertificateDetails> certificateDetailsGenericRepository, IGenericRepository<EntityFW.Entity.CertificateStandard> certificateStandardGenericRepository

                                        )
        {
            _commandExecutor = commandExecutor;
            _certificateGenericRepository = certificateGenericRepository;
            _customerGenericRepository = customerGenericRepository;
            _equipmentGenericRepository = equipmentGenericRepository;
            _certificateDetailsGenericRepository = certificateDetailsGenericRepository;
            _certificateStandardGenericRepository = certificateStandardGenericRepository;
        }

        public SearchResult<CertificateDTO> Search(SearchCriteria.CertificateSearchCriteria searchCriteria, int pageSize, int pageIndex)
        {
            return _commandExecutor.Execute(new GetCertificateDTOBySearchCriteriaDbCommand(searchCriteria, pageSize, pageIndex));
        }

        public CreateNewCertificateCommand SearchById(int id)
        {
            var certificate = _certificateGenericRepository.GetByID(id);

            var mainCertificate = Mapper.Map<EntityFW.Entity.Certificate, CreateNewCertificateCommand>(certificate);

            var certificateDetails = _certificateDetailsGenericRepository.Get(n => n.CertificateId == id);

            var listMainIds = certificateDetails.Select(n => n.CertificateStandardId);

            var customer = _customerGenericRepository.GetByID(certificate.CustomerId);

            var mainCustomer = Mapper.Map<EntityFW.Entity.Customer, TransactionCustomerModel>(customer);

            var equipment = _equipmentGenericRepository.GetByID(certificate.EquipmentId);

            var certificateStandards = _certificateStandardGenericRepository.Get(n => listMainIds.Contains(n.CertificateStandardId));

            var standards = Mapper.Map<List<EntityFW.Entity.CertificateStandard>, List<StandardModel>>(certificateStandards.ToList());

            mainCertificate.Customer = mainCustomer;
            mainCertificate.Standards = standards;
            return mainCertificate;
        }
    }
}
